package com.easyoffer.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.easyoffer.common.BaseResponse;
import com.easyoffer.common.ErrorCode;
import com.easyoffer.common.util.ResultUtils;
import com.easyoffer.constant.UserConstant;
import com.easyoffer.model.dto.user.UserLoginRequest;
import com.easyoffer.model.dto.user.UserRegisterRequest;
import com.easyoffer.model.dto.user.UserUpdateRequest;
import com.easyoffer.model.vo.UserVO;
import com.easyoffer.service.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/hello")
    public void hello() {
        // 1.5.0 版本开始可以直接利用 try-with-resources 特性
        try (Entry entry = SphU.entry("HelloWorld")) {
            // 被保护的逻辑
            System.out.println("hello world");
        } catch (BlockException ex) {
            // 处理被流控的逻辑
            System.out.println("blocked!");
        }
    }
    /**
     *
     * @param registerRequest 注册用户信息
     * @return 注册成功的用户id
     */
    @PostMapping("/register")
    BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest registerRequest){
        if (registerRequest == null)
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        return userService.userRegister(registerRequest);
    }

    /**
     * 登录
     * @param loginRequest 登录信息
     * @param request HttpServletRequest
     * @return 用户信息(待优化成UserVO)
     */
    // TODO: 2025-03-15
    @PostMapping("/login")
    BaseResponse<UserVO> userLogin(@RequestBody UserLoginRequest loginRequest, HttpServletRequest request){
        if (loginRequest == null)
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        System.out.println(loginRequest);
        String userAccount = loginRequest.getUserAccount();
        String userPassword = loginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword))
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        return userService.userLogin(loginRequest, request);
   }

    /**
     * 退出
     * @param request HttpServletRequest
     * @return 无权限异常 退出失败 1 退出成功
     */
   @PostMapping("/logout")
   BaseResponse<Integer> userLogout(HttpServletRequest request){
        if ( !StpUtil.isLogin() )
            return ResultUtils.error(ErrorCode.NO_AUTH_ERROR,"未登录");
        StpUtil.logout();
        if ( StpUtil.isLogin() )
            return ResultUtils.error(ErrorCode.NO_AUTH_ERROR,"退出失败");
        return ResultUtils.success(1);
   }

    /**
     * 用户签到
     * @param request HttpServletRequest
     * @return 是否签到成功
     */
   @PostMapping("/sign-in")
   BaseResponse<Boolean> userSignIn(HttpServletRequest request){
       //必须登录才能签到
       UserVO userVO = (UserVO) StpUtil.getSession().get(UserConstant.USER_LOGIN_STATE);
       if (ObjectUtils.isEmpty(userVO))
           return ResultUtils.error(ErrorCode.NO_AUTH_ERROR,"未登录");
       Boolean result =userService.userSignIn(userVO.getId(),request);
       return ResultUtils.success(result);
   }

   @GetMapping("/get/sign-in")
   @SaCheckLogin
   BaseResponse<List<Integer>> getUserSignIn(Integer year,Long userId,HttpServletRequest request){
       UserVO userVO = (UserVO) (UserVO) StpUtil.getSession().get(UserConstant.USER_LOGIN_STATE);
       List<Integer> result = userService.getUserSignInRecord(year,userId,request);
       return ResultUtils.success(result);
   }



    /**
     * 更新用户信息
     * @param updateRequest 更新用户信息
     * @param request HttpServletRequest
     * @return 是否更新成功 1 成功
     * @return
     */
   @PostMapping("/update")
   BaseResponse<Integer> updateUser(@RequestBody UserUpdateRequest updateRequest, HttpServletRequest request){
       if (updateRequest == null)
           return ResultUtils.error(ErrorCode.PARAMS_ERROR);
       return userService.updateUser(updateRequest, request);
   }
}
